﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace WindowsSignInPage
{
    public class EncryptTool
    {
        private static string key = "cibesedu";
        public static string Encrypt(string text)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.UTF8.GetBytes(text);
            dESCryptoServiceProvider.Key = (Encoding.ASCII.GetBytes(key));
            dESCryptoServiceProvider.IV = (Encoding.ASCII.GetBytes(key));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] array = memoryStream.ToArray();
            return Convert.ToBase64String(array);
        }

        public static string Decrypt(string text)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] array = Convert.FromBase64String(text);
            dESCryptoServiceProvider.Key = (Encoding.ASCII.GetBytes(key));
            dESCryptoServiceProvider.IV = (Encoding.ASCII.GetBytes(key));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            return Encoding.UTF8.GetString(memoryStream.ToArray());
        }
    }
}
